package org.anupta.game.server.game.packet.account.character.selected;

import org.anupta.game.entity.dao.DAO;
import org.anupta.game.entity.entities.Player;
import org.anupta.game.main.Main;
import org.anupta.game.server.game.client.GameClient;
import org.anupta.game.server.game.packet.GamePacket;
import org.anupta.game.server.game.packet.account.character.create.answer.TutorialBegin;
import org.anupta.game.server.game.packet.account.character.selected.answer.CharacterJoin;
import org.anupta.game.server.game.packet.account.character.selected.answer.CharacterSelectedError;

public class CharacterSelected extends GamePacket {
	private int playerId = 0;

	public CharacterSelected(GameClient client, String message) {
		super(client, message);
		unpack();
		doPacket();
	}

	@Override
	public void unpack() {
		playerId = Integer.parseInt(packetIn.substring(2));
	}

	@Override
	public void pack() {
	}

	@Override
	public void doPacket() {
		if(!client.getAccount().getPlayers().containsKey(playerId)){
			new CharacterSelectedError(client);
			return;
		}
		Player player = client.getAccount().getPlayers().get(playerId);
		if(player != null){
			client.setPlayer(player);
			Main.out.println(new StringBuilder("Player selected : ").append(playerId).append(" from : ").append(client.getIp()).toString());
			if(client.getPlayer().isNew()){
				client.getPlayer().setNew(false);
				DAO.getDao(Player.class).update(player);
				new TutorialBegin(client);
			}
			new CharacterJoin(client);
		}
	}
}
